package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import com.atguigu.springcloud.lb.LoadBalancer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;

/**
 * @author xiaolong
 * @version 1.0
 * @description: TODO
 * @date 2021/9/2 17:31
 */
@RestController
@Slf4j
public class OrderController {
    //单机可以直接写死
//    public static final String URL = "http://localhost:8001";
    //集通过eureka中的注册的服务名去找到对应的主机
    public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";


    //restTemplate是springboot对httpclient的封装
    @Autowired
    private RestTemplate restTemplate;

    //手写轮询算法
    @Autowired
    private LoadBalancer loadBalancer;
    //服务发现
    @Autowired
    private DiscoveryClient discoveryClient;


    /**
     * @param
     * @return
     * @description: 添加记录
     */
    @GetMapping("/consumer/payment/create")
    public CommonResult<Payment> create(Payment payment) {
        /**
         * postForObject 提供post请求  payment默认传参是json类型
         * @param1 URL 访问http请求
         * @param2 参数
         * @param3 返回值
         */

        return restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, CommonResult.class);
    }


    /**
     * getForObject: get请求查询记录
     *
     * @param
     * @return
     */
    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") Long id) {
        return restTemplate.getForObject(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
    }


    /**
     * @param
     * @return
     * @description: getForEntity 可以返回响应体的内容
     */
    @GetMapping("/consumer/payment/getForEntity/{id}")
    public CommonResult<Payment> getPayment2(@PathVariable("id") Long id) {

        ResponseEntity<CommonResult> entity = restTemplate.getForEntity(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);

        //http编码为200
        if (entity.getStatusCode().is2xxSuccessful()) {
            //返回请求体
            return entity.getBody();
        } else {
            return new CommonResult<Payment>(444, "操作失败");
        }
    }

    /**
     * @param
     * @return
     * @description: 自定义的负载均衡轮询算法
     */
    @GetMapping(value = "/consumer/payment/lb")
    public String getPaymentLB() {

        //获取服务名下注册的所有信息（各个主机的相关信息）
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        if (instances == null || instances.size() <= 0) {
            return null;
        }
        ServiceInstance serviceInstance = loadBalancer.instances(instances);
        //主机的ip+端口 url
        URI uri = serviceInstance.getUri();
        return restTemplate.getForObject(uri + "/payment/lb", String.class);
    }


    @GetMapping("/consumer/payment/zipkin")
    public String paymentZipkin() {
        String result = restTemplate.getForObject(PAYMENT_URL + "/payment/zipkin/", String.class);
        return result;
    }


}
